$(document).ready(function() {
    $("#socialAuthVkontakte").click(function() {
        client_id = document.getElementById("socAuth_vk_client_id").value;
        display = document.getElementById("socAuth_vk_display").value;
        redirect_uri = document.getElementById("socAuth_vk_redirect_uri").value;
        NewWindow = window.open("http://api.vk.com/oauth/authorize?client_id=" + client_id + "&scope=offline&display=" + display + "&redirect_uri=" + redirect_uri + "?network=vk", "AuthWindow", "width=900,height=500,left=200,top=100");
        NewWindow.focus();
        return false;
    });

    $("img#socialAuthTwitter").click(function() {
        our_site = document.getElementById("socAuth_our_site").value;
        NewWindow = window.open(our_site, "AuthWindow", "width=900,height=500,left=200,top=100");
        NewWindow.focus();
    });

    $("#socialAuthFacebook, .js-social-auth-facebook").click(function() {
        client_id = document.getElementById("socAuth_facebook_client_id").value;
        redirect_uri = document.getElementById("socAuth_facebook_redirect_uri").value;
        NewWindow = window.open("https://www.facebook.com/dialog/oauth?client_id=" + client_id + "&redirect_uri=" + redirect_uri + "&scope=email,user_birthday,user_location,user_interests,user_checkins,user_photos,user_status", "AuthWindow", "width=900,height=500,left=200,top=100");
        NewWindow.focus();
        return false;
    });

    $("#registerYes").click(function() {
        document.getElementById("registerForEnd").style.display = "none";
        document.getElementById("registerForEndNo").style.display = "none";
        document.getElementById("registerForEndYes").style.display = "block";
    });

    $("#registerNo").click(function() {
        document.getElementById("registerForEnd").style.display = "none";
        document.getElementById("registerForEndYes").style.display = "none";
        document.getElementById("registerForEndNo").style.display = "block";
    });

    $(".registerBack").click(function() {
        document.getElementById("registerForEnd").style.display = "block";
        document.getElementById("registerForEndYes").style.display = "none";
        document.getElementById("registerForEndNo").style.display = "none";
    });

    $("#registerYesLogin").click(function() {
        if ($('#yesPassword').val() == '' || $('#yesEmail').val() == '') {
            $('div#registerForEndYesResult').html(registerRequire + "<br/><br/>");
        }
        else {
            $('div#registerForEndYesResult').html('<img src="/static/images/siteProof/loader_light.gif" alt="" border=""/><br/><br/>');
            document.getElementById("registerYesLogin").disabled = true;

            $.ajax({
                'url': '/socialnetworksauth/default/authorize',
                'type': 'POST',
                'data': {
                    'email': $('#yesEmail').val(),
                    'password': $('#yesPassword').val(),
                    'network': $('#yesNetwork').val(),
                    'uid': $('#yesUid').val(),
                    'json': $('#yesJson').val(),
                    'end': $('#yesEnd').val(),
                    'sex': $('input:radio[name=noSex]:checked').val()
                },
                'cache': false,
                'success': function(response) {
                    document.getElementById("registerYesLogin").disabled = false;
                    var data = eval("(" + response + ")");

                    if (data.response == 0) {
                        $('div#registerForEndYesResult').html(registerRequire + "<br/><br/>");
                    }
                    else if (data.response == 1) {
                        //$('div#registerForEndYesResult').html(data.text+" <a href='/'>"+data.here+"</a>"+"<br/><br/>");
                        document.location.href = data.redirect;
                    }
                    else if (data.response == 2) {
                        $('div#registerForEndYesResult').html(data.text + "<br/><br/>");
                    }
                },
                'error': function() {
                    document.getElementById("registerYesLogin").disabled = false;
                    $('div#registerForEndYesResult').html(registerFailed + "<br/><br/>");
                }
            });
        }
    });

    $("#registerNoLogin").click(function() {
        if ($('#noName').val() == '' || $('#noEmail').val() == '') {
            $('div#registerForEndNoResult').html(registerRequire + "<br/><br/>");
        }
        else {
            $('div#registerForEndNoResult').html('<img src="/static/images/siteProof/loader_light.gif" alt="" border=""/><br/><br/>');
            document.getElementById("registerNoLogin").disabled = true;

            $.ajax({
                'url': '/socialnetworksauth/default/register',
                'type': 'POST',
                'data': {
                    'email': $('#noEmail').val(),
                    'json': $('#noJson').val(),
                    'name': $('#noName').val(),
                    'network': $('#yesNetwork').val(),
                    'uid': $('#yesUid').val(),
                    'sex': $('#noSex').val(),
                    'country': $('#noCountry').val(),
                    'city': $('#noCity').val(),
                    'end': $('#noEnd').val(),
                    'age': $('#noAge').val()
                },
                'cache': false,
                'success': function(response) {
                    document.getElementById("registerNoLogin").disabled = false;
                    var data = eval("(" + response + ")");

                    if (data.response == 0) {
                        $('div#registerForEndNoResult').html(registerRequire + "<br/><br/>");
                    }
                    else if (data.response == 1) {
                        //$('div#registerForEndNoResult').html(data.text+" <a href='/'>"+data.here+"</a>"+"<br/><br/>");
                        document.location.href = data.redirect;
                    }
                    else if (data.response == 2) {
                        $('div#registerForEndNoResult').html(data.text + "<br/><br/>");
                    }
                },
                'error': function() {
                    document.getElementById("registerNoLogin").disabled = false;
                    $('div#registerForEndNoResult').html(registerFailed + "<br/><br/>");
                }
            });
        }
    });
    $('#country').change();
});
